<template>
    <div>
        <el-dialog title="修改密码" :visible="passShow" width="25%" @close="dialogClose">
            <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" lable="left" label-width="auto"
                class="demo-ruleForm">
                <div class="content">
                    <el-form-item label="手机号" prop="tel">
                        <el-input v-model="ruleForm.tel" placeholder=""></el-input>
                    </el-form-item>
                    <!-- 验证码 -->
                    <el-form-item label="验证码" prop="smsCode">
                        <div style="display:flex;justify-content:space-between">
                            <el-input v-model="ruleForm.smsCode" placeholder="请输入6位验证码" maxlength="6" show-word-limit
                                class="input_width" style="width:70%;"></el-input>
                            <el-button v-bind:type="button_sms_type" v-on:click="sendSmsCode"
                                v-bind:disabled="button_sms_disable">
                                {{button_sms_value}}</el-button>
                        </div>

                    </el-form-item>
                    <el-form-item label="新密码" prop="password">
                        <el-input type="password" v-model="ruleForm.password" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="确认密码" prop="checkPass">
                        <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" class="pull-right margin-l-25" @click="dialogClose">返回</el-button>
                        <el-button type="primary" style="margin:0 auto" @click="submitForm" class="pull-right">提交
                        </el-button>
                    </el-form-item>
                </div>

            </el-form>
        </el-dialog>
    </div>
</template>

<script>
export default {
    //接受父组件
    props: {
        passShow: {
            type: Boolean
        }
    },

    data () {
        var validatePass = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请输入密码'));
            } else {
                if (this.ruleForm.checkPass !== '') {
                    this.$refs.ruleForm.validateField('checkPass');
                }
                callback();
            }
        };
        var validatePass2 = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请再次输入密码'));
            } else if (value !== this.ruleForm.password) {
                callback(new Error('两次输入密码不一致!'));
            } else {
                callback();
            }
        }

        return {
            button_sms_type: 'success',
            button_sms_value: '验证码',
            button_sms_disable: false,
            sms_time_count: 60,
            sms_timer: null,
            ruleForm: {
                tel: '',
                smsCode: '',
                password: '',
                checkPass: ''
            },
            rules: {
                tel: [{
                    required: true,
                    message: '请输入手机号',
                    trigger: 'blur'
                }],
                smsCode: [{
                    required: true,
                    message: '请输入验证码',
                    trigger: 'blur'
                }],
                password: [
                    { validator: validatePass, trigger: 'blur' }
                ],
                checkPass: [
                    { validator: validatePass2, trigger: 'blur' }
                ],

            },
        }
    },
    methods: {

        dialogClose () {
            this.$emit('dialogClose')
        },
        submitForm () {
            this.$refs.ruleForm.validate((valid) => {
                if (valid) {
                    alert('submit!');
                    this.$emit('dialogClose')
                }
                else {
                    return
                }
            });
        },

    },
}
</script>

<style lang="less" scoped>
/deep/ .el-form-item__label {
  font-size: 13px;
  color: #555;
}
</style>